import urllib.request

# 百度翻译API 详细翻译接口
BaiduTranslateAPI = 'https://fanyi.baidu.com/v2transapi'
Method = 'POST'
header = {
    'Accept': '*/*',
    # 'Accept-Encoding': ' gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Acs-Token': '1657263608264_1657322833250_+eLAZM/2Jp9yxeKuloZCwKPN2i1h9trnfPbWP7/1g78dpEPmuTV083g0hHRXx+R+Ke9tmWAOpfQbOk1D3wTqvp7bOsnHmXKV8StDxYRQjHEUKHFLVE0yoUqa3BWWyeFZga5rBtn/jTC5Nmuzx7EtXycjzR7IJwKikVO7SztHdgITQrORXqB9Ix23KkuHd0kbP/AycWu9S8NQefmtQf9U0g3rGKhDcqGnY7mYqNSoFkr5RVzknJS4uDPwPbj1nsX9Uz2J3w3yiDwsAUBzURGF7m8gifOHI7vYJTjkmNnccNdgCDz8CdgUZvAZnD8wAQBY',
    'Connection': 'keep-alive',
    'Content-Length': '135',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie': 'REALTIME_TRANS_SWITCH=1; SOUND_SPD_SWITCH=1; HISTORY_SWITCH=1; FANYI_WORD_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=B560708BACE4C1F077C16691ECEB7D41:FG=1; PSTM=1655816676; BIDUPSID=5A53AA79514268FC9BF0F425F513A68B; APPGUIDE_10_0_2=1; ZFY=V4XbWbhTd0jRTmFHZe:BnCPp5jrd58XG39HgSxB:A8iV0:C; BA_HECTOR=24202ha5050l25ah2521k0601hcgik117; BDRCVFR[WPpCI9gPPSY]=mk3SLVN4HKm; delPer=0; PSINO=6; H_PS_PSSID=31660_26350; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BDSFRCVID=qU0OJexroG062LOD1WJ8JCaSleKKx9nTDYrEOwXPsp3LGJLVc_VhEG0PtEzdRF4M8lr_ogKKQgOTHRCF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BDSFRCVID_BFESS=qU0OJexroG062LOD1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mb67JD-50eGLeqT_eJb-s3bRObR-_HRjYbb__-P4DepJRexRZ5mAqoJR8XKoWHpb9MbDW3qLJQHjXLqoD5I7naIQqahOYfhjOKT3sKRkV-nbbKRQ43bRTsxKy5KJvEj6bDCcjhP-UynQMWh37Le7lMKoaMp78jR093JO4y4Ldj4oxJpOJ5JbMonLafD8WhCI9D6tben-W5gTKbPcha5-X3buQypvM8pcNLTDKWxnXDxba0nv3bevP-RnNJJ0MetnJ5lO1j4_eyJJrJfnaagIfs4O2Bqn5Kq5jDh0Kb6ksD-RtW6JZL57y0hvcLR3cShn-LUjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t2b6QhDN8tt6-ttbKs3t88KJjEe-Kk-PnVen_AD-nZKRvHa2kj_Do1Bh-aSpF9yxbH0PA8yPROq46n3N5HKl75yUJ5qKOsQU6d34tm3x7405OTbgTE0UoIJpCWD-o3hPJvyp_sXnO7QfJlXbrtXp7_2J0WStbKy4oTjxL1Db3JKjvMtgDtVJO-KKC-MK-mef2; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDRCVFR[tox4WRQ4-Km]=mk3SLVN4HKm; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1656213799,1656818948,1657278958,1657322825; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1657323572; ab_sr=1.0.1_ZjU0ZWQ5NmE5MWUxYjdhOTkwOWZhYTkyMzI2MWFjZWQ5NWU5NmJlMDZkN2RkNjExNzY4MjFmODA1OWY2ZjlmYzc5NDQwMTZlNDQ2MDAzMzE4NTlhYmE0OWMxMDczZTk0OGEwNjNmNjA4OTIyNGQ0YTNkYzQ0YzI5NWVhNWNkOWE3ODNiNTA3NTM5MjI5NmNkNDViN2E4ZmU0NWRmMTM3NQ==',
    'Host': 'fanyi.baidu.com',
    'Origin': 'https://fanyi.baidu.com',
    'Referer': 'https://fanyi.baidu.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'
}

# 请求参数，正则快速添加引号 https://blog.csdn.net/wang_hugh/article/details/81043852
# (.*?):(.*)
# '$1': '$2',
FormData = {
    'from': 'en',
    'to': 'zh',
    'query': 'love',
    'transtype': 'translang',
    'simple_means_flag': '3',
    'sign': ' 393791.188686',
    'token': 'f6d3d9e46236f3d49ed193975cb60a4a',
    'domain': 'common'
}

# 封装请求对象
FormData = urllib.parse.urlencode(FormData).encode('UTF-8')
request = urllib.request.Request(url=BaiduTranslateAPI, headers=header, data=FormData)
response = urllib.request.urlopen(request)
result = response.read().decode('UTF-8')
print(result)
